<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Core.php");
class CI_Articlecategory extends CI_Core{
	function __construct(){
		parent::__construct();
		$this->database('default');
	}
	//文章分类列表
	public function getlist(){
		$pid = $this->ci->input->get('pid');
		$self = $this->ci->input->get('self');
		$params = array('parent_id'=>empty($pid)?"'0'":$pid);
		if($self) $params['id !=']=$self;
		$arr = $this->get_all('*',$params,'article_category');
		$rs=array();
		foreach($arr as $v){
			$a = $this->get_all('*',array('parent_id'=>$v['id']),'article_category');
			$rs[]=array(
				'id'=>$v['id'],
				'label'=>$v['name'],
				'branch'=>array(),
				'inode'=>count($a)>0?true:false,
				'type'=>$v['external_url'],
				'no_del'=>$v['no_del'],
				'no_edit'=>$v['no_edit'],
			);
		}
		return $this->status(true, $rs);
	}
	
	private function showType($v){
		if($v==1) return '网页列表';
		if($v==2) return '微信图文';
		if($v==3) return '外部链接';
		if($v==4) return '微信文本';
	}
	//自由查询文章
	public function pub_search($tj=array(),$tj2=array()){
		$arr = $this->get_all('*',$tj,'article_category',$tj2);
		return $this->status(true, $arr);
	}
	//当前分类下子分类
	public  function getCategory($pid,&$ids){ 
		$arr=array_pop($this->pub_search(array('parent_id'=>$pid)));
		if($arr==array()) return;
		else{
			foreach($arr as $v){
				$ids[]=$v['id'];
				$this->getCategory($v['id'],$ids);
			}
		}
	}
	//创建文章分类
	public function create(){
		
	}
	//删除文章分类
	public function remove(){
		$id = $this->ci->input->get('id');
		$arr = $this->get_all('id',array('parent_id'=>$id),'article_category');
		$arr = array_map('implode',$arr);
		$arr[]=$id;
		$this->db->where_in('id', $arr);
		$this->db->delete('article_category');
	}
	//更新文章分类
	public function update($id){
		$id=$id?$id:$this->ci->input->get('id');
		$rs=$this->get_one('*',array('id'=>$id),'article_category');
		if($rs['parent_id']!=0){
			$arr = $this->get_one('*',array('id'=>$rs['parent_id']),'article_category');
			$rs['cateName']=$arr['name'];
		}
		return $this->status(true, $rs);
	}
	//按分类拼音查询
	public function getByPinyin($v){
		$rs=$this->get_one('*',array('en_name'=>$v),'article_category');
		return $this->status(true, $rs);
	}
	//保存
	public function save(){
		$arr = $_POST;
		/**if($arr['parent_id']==0 || !$arr['parent_id']){
			$arr['ptree']='';
		}else{
			$a = array($arr['parent_id']);
			$this->getParentCategory($arr['parent_id'],$a);
			$arr['ptree']=implode(',',array_reverse($a));
		}**/
		$arr['site_id']=$this->ci->session->userdata('wechat_id');
		$this->write($arr,'article_category');
	}
	//根据当前ID往上找父级
	public function getParentCategory($id,&$arr){
		$rs = $this->get_one('*',array('id'=>$id),'article_category');
		if($rs['parent_id']) $arr[]=$rs['parent_id'];
		if($rs['parent_id']==0){
			return;
		}else{
			$this->getParentCategory($rs['parent_id'],$arr);
		}
	}
	//根据夫级ID查子类
	public function getSubCategory($id){
		$id=$id?$id:$this->ci->input->get('id');
		$arr = $this->get_all('*',array('parent_id'=>$id),'article_category');
		return $this->status(true, $arr);
	}
	
}